package com.cc.web;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;

import java.sql.Time;
import java.util.concurrent.TimeUnit;

/**
 * 编写封装selenium进行web自动化,依赖：selenium-java
 * @author cc
 * @create 2020-04-26-16:52
 */
public class Web {
    //驱动浏览器的driver
    private WebDriver driver;

    /**
     * 打开浏览器默认是chrome
     * @param type 浏览器类型
     */
    public void openBrawer(String type){
        switch (type){
            case "chrome":
                System.setProperty("webdriver.chrome.driver","driver.chromedriver.exe");
                ChromeOptions option = new ChromeOptions();
                option.addArguments("--user-data-dir=D:\\chromedata");
                option.addArguments("--start-maximized");
                driver = new ChromeDriver(option);
                driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                break;
            case "firfox":
                System.setProperty("webdriver.firefox.bin", "C:\\Program Files\\Firefox\\firefox.exe");
                System.setProperty("webdriver.gecko.driver", "Drivers/geckodriver.exe");

                driver = new FirefoxDriver();
                driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                break;
            default:
                System.setProperty("webdriver.chrome.driver","driver.chromedriver.exe");
                ChromeOptions option1 = new ChromeOptions();
                option1.addArguments("--user-data-dir=D:\\chromedata");
                option1.addArguments("--start-maximized");
                driver = new ChromeDriver(option1);
                driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        }


    }


    private WebElement findEle(String xpath){
        WebElement ele = driver.findElement(By.xpath(xpath));
        return ele;
    }

    public void vistWeb(String url){
        driver.get(url);

    }

    public void click(String xpath){
        findEle(xpath).click();

    }




}
